import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_applearnprogrom/http/diohttp/HttpRequest.dart';
import 'package:flutter_applearnprogrom/video/viewmodelbywangyi/BannerItem.dart';

/****
 *
 *
 * 有许多的widget 需要放置需要存放.text
 */
class DiscoverBanner extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<DiscoverBanner> {
//   首尾 都要补一张
  PageController _pageController = PageController(initialPage: 1); // 第一张开始
//定时器
  int _currentIndex = 1;
  Timer _timer;

  //  存放项目数据
  List _banners = [];

  @override
  void initState()  {
//    HttpRequest httpRequest = new HttpRequest();
//    httpRequest.get('/banner', queryParameters: {'type': 2}).then((response) {
//      print('网络请求' + response);
//      List banners = response.data['banners']
//          .map((banner) => BannerItem(pic: banner['pic']))
//          .toList();
//
////     设置数据
//      setState(() {
//        _banners = banners;
//      });
//    });
//
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
//    22.3.31
//  添加数据到集合
    List addedBanners = [];
    addedBanners.add(_banners[_banners.length - 1]
      ..addAll(_banners)
      ..add(_banners[0]));
    return SliverToBoxAdapter(
      //顶部下拉效果

      child: AspectRatio(
        aspectRatio: 2.5,
        child: Stack(
          children: <Widget>[
            PageView(
//              回弹效果22.3.31
                physics: BouncingScrollPhysics(),
                controller: _pageController,
                onPageChanged: (page) {},
//                children: _banners  数据 被替换
                children: addedBanners
                    .map((item) => Container(
                          margin: EdgeInsets.all(10.0),
                          // 有圆角需要裁剪
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(5.0),
                            child: Image.network(
                              item.pic,
                              fit: BoxFit.cover,
                            ),
                          ),
                        ))
                    .toList())
          ],
        ),
      ),
    );
  }
}
